package cn.rengy.web.framework.wxmp;

import java.util.Map;
import java.util.concurrent.Callable;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.rengy.web.framework.cache.CacheService;
import cn.rengy.web.framework.sql.DbService;
import cn.rengy.web.framework.sys.pub.upload.service.PubFileService;

@Service
public class WxMerchantService {
	private static Logger logger = LoggerFactory.getLogger(WxMerchantService.class);
	@Autowired
	private DbService dbService;
	@Autowired
	private PubFileService pubFileService;
	
	@Autowired
	private CacheService cacheService;
	
	@Transactional
	public void add(Map<String,Object> param) {
		Long id=MapUtils.getLong(param, "mch_id");
		//sql执行时会清除缓存
		this.dbService.updateMapParameter("insert_wx_merchant", param);
		Long cert_id=MapUtils.getLong(param, "cert_id");
		this.pubFileService.saveSingle(16L, id, cert_id);
	}
	public void update(Map<String,Object> param){
		Long id=MapUtils.getLong(param, "mch_id");
		//sql执行时会清除缓存
		this.dbService.updateMapParameter("update_wx_merchant", param);
		Long cert_id=MapUtils.getLong(param, "cert_id");
		this.pubFileService.saveSingle(16L, id, cert_id);
	}
	/*public String getMchId() {
		return this.propertyService.getProperty("mch_id");
	}*/
	/**
	 * 为了快速访问，放到本地缓存
	 * @param mch_id
	 * @return
	 */
	public WxMerchant getWxMerchant(String mch_id) {
		return cacheService.getLocalCache("wxMerchantCache", mch_id, new Callable<WxMerchant>() {
	        @Override
	        public WxMerchant call() throws Exception {
				return dbService.queryForBean("select_get_wx_merchant", WxMerchant.class, mch_id);
	        }
	    });
	}
	
	public void enable(Map<String,Object> param) {
		String mch_id=MapUtils.getString(param, "mch_id");
		this.dbService.update("disable_wx_merchant",0, mch_id);
	}
	
	public void disable(Map<String,Object> param) {
		String mch_id=MapUtils.getString(param, "mch_id");
		this.dbService.update("disable_wx_merchant",1, mch_id);
		//删除缓存
		cacheService.evictLocalCache("wxMerchantCache", mch_id);
		
	}
}
